/*
 * @Author: rogi
 * @Date: 2022-07-27 12:32:57
 * @LastEditTime: 2022-12-12 19:59:42
 * @LastEditors: rogi
 * @Description: 描述
 */
import axios from 'axios';
import qs from 'qs';
import Cookie from 'js-cookie';
import { host } from '@/utils/config.js'
import { ElNotification } from 'element-plus';
import store from '../store/index.js';
import { write,read,remove} from "@/utils/storage";



const instance = axios.create({
  // baseURL: host.requestHost + '/jeecg-boot/',
  // baseURL: 'http://192.168.1.34:9202/',
  baseURL: host.requestHost,

  timeout: 5000,
});


const err = (error) => {
  if (error.response) {
    let data = error.response.data
    const token = read('X-Access-Token')
    switch (error.response.status) {
      case 404:
        ElNotification({
          title: '很抱歉，资源未找到!',
          type: 'error',
        })
        break
      case 504:
        ElNotification({
          title: '网络超时!',
          type: 'error',
        })
        break

      case 401:
        ElNotification({
          title: 'token未授权，请重新登录',
          type: 'error',
        })
        if (token) {
          store.commit('common/USER_LOGIN_OUT')
        }
        break
    }
  }
  return Promise.reject(error)
}


// 请求拦截器
instance.interceptors.request.use(config => {
  const token = read('X-Access-Token')
  // if (token) {
  //   config.headers['X-Access-Token'] = token
  // }
  config.headers['Authorization'] = sessionStorage.getItem('access_token')
  config.headers['enterprise_id'] = sessionStorage.getItem('enterprise_id')
  config.headers['expires_in'] = sessionStorage.getItem('expires_in')
  config.headers['user_id'] = sessionStorage.getItem('user_id')
  config.headers['user_type'] = sessionStorage.getItem('user_type')
  return config;
}, (error) => {
  return Promise.reject(error)
}
);

instance.interceptors.response.use(response => {
  if(response.data.code === 200 || response.data.code === 0) return response.data.result || response.data.data;
  else return response.data
  
}, err);

// get 方式
export function get(url, params) {
  return new Promise((resolve, reject) => {
    instance.get(url, {
      params
    }).then(res => {
      resolve(res);
    }).catch(err => {
      reject(err);
    });
  });
}

// 传参格式为 form-data
export function post(url, params) {
  return new Promise((resolve, reject) => {
    instance.post(url, qs.stringify(params)).then(res => {
      resolve(res);
    }).catch(err => {
      reject(err);
    });
  });
}



// 传参格式为 json
export function postObj(url, params) {
  return new Promise((resolve, reject) => {
    console.log(resolve);
    instance.post(url, params).then(res => {
      resolve(res);
    }).catch(err => {
      reject(err);
    });
  });
}


//put请求
export function put(url, params) {
  return new Promise((resolve, reject) => {
    instance.put(url, qs.stringify(params)).then(res => {
      resolve(res);
    }).catch(err => {
      reject(err);
    });
  });
}

export function putAction(url, params) {
  return new Promise((resolve, reject) => {
    instance.put(url, params).then(res => {
      resolve(res);
    }).catch(err => {
      reject(err);
    });
  });
}

//delete
export function Delete(url, params) {
  return new Promise((resolve, reject) => {
    instance.delete(url, {params}).then(res => {
      resolve(res);
    }).catch(err => {
      reject(err);
    });
  });
}


//postFile
export function postFile(url, params) {
  // debugger
  return new Promise((resolve, reject) => {
    // debugger
    instance.post(url,{params}, { headers: { 'X-Access-Token': read('X-Access-Token')} }).then(res => {
      // debugger
      resolve(res);
    }).catch(err => {
      reject(err);
    });
  });
}